package com.dhecp.project.system.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.utils.StringUtils;
import com.dhecp.core.constant.DataFiltering;
import com.dhecp.core.utils.PageUtils;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.system.domain.SysModuleSet;
import com.dhecp.project.system.mapper.SysModuleSetMapper;
import com.dhecp.project.system.service.SysDictionaryForFieldService;
import com.dhecp.project.system.service.SysModuleSetService;

/**
 * 功能设置  业务实现层
 * @author DH-lwl
 * @version 2019年11月18日
 */
@Service
public class SysModuleSetServiceImpl implements SysModuleSetService {
	
	@Autowired
	private SysModuleSetMapper moduleSetMapper;

	@Autowired
	private SysDictionaryForFieldService dictionaryForFieldService;
	
	/**
	 * 查询功能设置列表
	 * @param params
	 * @return List-ModuleSet
	 * @author DH-lwl
	 * @throws Exception 
	 */
	@Override
	public List<SysModuleSet> queryModuleSet(Map<String, Object> params) throws Exception {
		params.put(DataFiltering.DATA_WHERE, dictionaryForFieldService.getWhereStr(params));
		params.put("userId", ShiroUtils.getUserId());
		if (params.get("field") == null || params.get("field") == ""){
			String orderBy = "menuGuid desc, moduleOrder asc";
			PageUtils.startPageOrder(orderBy);
		}else{
			PageUtils.startPage();	
		}	
		return moduleSetMapper.findModuleSet(params);
	}

	/**
	 * 通过guid查询单条记录
	 * @param guid
	 * @return 实体SysModuleSet
	 * @author DH-lwl
	 */
	@Override
	public SysModuleSet queryModuleSetByGuid(String guid) {
		return moduleSetMapper.findModuleSetByGuid(guid);
	}

	/**
	 * 通过moduleTable查询单条记录
	 * @param moduleTable
	 * @return 实体SysModuleSet
	 * @author DH-Xxj
	 */
	@Override
	public SysModuleSet queryModuleSetByModuleTable(String moduleTable) {
		return moduleSetMapper.findModuleSetByModuleTable(moduleTable);
	}

	/**
	 * 通过guid修改功能设置
	 * @param moduleSetItems
	 * @return MessageModel
	 * @author DH-lwl
	 */
	@Override
	public AjaxResult updateModuleSetByGuid(Map<String, Object> params) {
		if (StringUtils.isEmpty(params)) {
			return AjaxResult.error("数据提交失败，必填参数有误！");
		}
		List<SysModuleSet> moduleSetList = JSON.parseArray(params.get("moduleSetItems").toString(), SysModuleSet.class);
		if (moduleSetList == null || moduleSetList.size() == 0) {
			return AjaxResult.error("数据包格式有误，请检查！");
		}
		for (SysModuleSet moduleSet : moduleSetList) {
			moduleSetMapper.updateByGuid(moduleSet);
		}
		return AjaxResult.success("操作成功");
	}

}
